package top.drinyeit.weilong.fragment;

import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scu.miomin.shswiperefresh.core.SHSwipeRefreshLayout;

import java.util.HashMap;
import java.util.Map;

import top.drinyeit.weilong.R;
import top.drinyeit.weilong.base.LazyFragment;
import top.drinyeit.weilong.presenter.ShopSearchResultPresenter;

/**
 * Created by wu on 2018/1/18.
 */

public class ShopSearchResultFragment extends LazyFragment {
    String searchKey;
    public RecyclerView mRecyclerView;
    private TextView score, sold;
    private RelativeLayout score_sort, sold_sort;
    private int priceNo = 0;
    private int soldNo = 0;
    public SHSwipeRefreshLayout shSwipeRefreshLayout;
    public int pageIndex;

    private ShopSearchResultPresenter shopSearchResultPresenter;


    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_category_shop;
    }

    @Override
    protected void initViews() {
        mRecyclerView = mRootView.findViewById(R.id.mRecyclerView);
        score = mRootView.findViewById(R.id.score);
        sold = mRootView.findViewById(R.id.sold);
        score_sort = mRootView.findViewById(R.id.score_sort);
        sold_sort = mRootView.findViewById(R.id.sold_sort);
        shSwipeRefreshLayout = mRootView.findViewById(R.id.srl_category_shop);
        initListener();
    }

    @Override
    protected void lazyLoad() {
        searchKey = getActivity().getIntent().getExtras().getString("search_key");
        shopSearchResultPresenter = new ShopSearchResultPresenter(this);
        Map<String, Object> map = new HashMap<>();
        map.put("action", "commodity_category_store");
        map.put("search_key", getActivity().getIntent().getExtras().getString("search_key"));
        map.put("page_index", 1);
        shopSearchResultPresenter.categoryStoreSearch(map);
    }

    private void initListener() {

        shSwipeRefreshLayout.setOnRefreshListener(new SHSwipeRefreshLayout.SHSOnRefreshListener() {
            @Override
            public void onRefresh() {
                Map<String, Object> map = new HashMap<>();
                map.put("action", "commodity_category_store");
                map.put("search_key", getActivity().getIntent().getExtras().getString("search_key"));
                if (priceNo == 0 && soldNo != 0) {
                    map.put("sell_type", soldNo);
                } else if (priceNo != 0 && soldNo == 0) {
                    map.put("price_type", priceNo);
                }
                map.put("page_index", 1);
                shopSearchResultPresenter.categoryStoreSearch(map);
            }

            @Override
            public void onLoading() {
                Map<String, Object> map = new HashMap<>();
                map.put("action", "commodity_category_store");
                map.put("search_key", getActivity().getIntent().getExtras().getString("search_key"));
                if (priceNo == 0 && soldNo != 0) {
                    map.put("sell_type", soldNo);
                } else if (priceNo != 0 && soldNo == 0) {
                    map.put("price_type", priceNo);
                }
                map.put("page_index", pageIndex + 1);
                shopSearchResultPresenter.categoryStoreSearch(map);
            }

            @Override
            public void onRefreshPulStateChange(float v, int i) {

            }

            @Override
            public void onLoadmorePullStateChange(float v, int i) {

            }
        });
        score_sort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scoreSort();
            }
        });

        sold_sort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                soldSort();
            }
        });
    }

    private void scoreSort() {
        sold.setText("销量");
        sold.setTextColor(ContextCompat.getColor(getActivity(), R.color.text_black_51));
        sold.setCompoundDrawables(null, null, null, null);
        Map<String, Object> map = new HashMap<>();
        map.put("action", "commodity_category_store");
        map.put("search_key", searchKey);
        Drawable drawable0 = ContextCompat.getDrawable(getActivity(), R.drawable.up_arrow_icon);
        Drawable drawable1 = ContextCompat.getDrawable(getActivity(), R.drawable.down_arrow_icon);
        drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
        drawable0.setBounds(0, 0, drawable0.getMinimumWidth(), drawable0.getMinimumHeight());
        if (priceNo == 0) {
            map.put("score_type", 2);
            map.put("page_index", 1);
            score.setTextColor(ContextCompat.getColor(getActivity(), R.color.app_green));
            score.setCompoundDrawables(null, null, drawable0, null);
            score.setText("高分优先");
            priceNo = 2;
            soldNo = 0;
            shopSearchResultPresenter.categoryStoreSearch(map);
        } else if (priceNo == 2) {
            map.put("score_type", 1);
            map.put("page_index", 1);
            score.setCompoundDrawables(null, null, drawable1, null);
            score.setText("低分优先");
            priceNo = 1;
            soldNo = 0;
            shopSearchResultPresenter.categoryStoreSearch(map);
        } else {
            map.put("score_type", 2);
            map.put("page_index", 1);
            score.setCompoundDrawables(null, null, drawable0, null);
            score.setText("高分优先");
            priceNo = 2;
            soldNo = 0;
            shopSearchResultPresenter.categoryStoreSearch(map);
        }
    }

    private void soldSort() {
        score.setText("评分");
        score.setTextColor(ContextCompat.getColor(getActivity(), R.color.text_black_51));
        score.setCompoundDrawables(null, null, null, null);
        Map<String, Object> map = new HashMap<>();
        map.put("action", "commodity_category_store");
        map.put("search_key", searchKey);
        Drawable drawable0 = ContextCompat.getDrawable(getActivity(), R.drawable.up_arrow_icon);
        Drawable drawable1 = ContextCompat.getDrawable(getActivity(), R.drawable.down_arrow_icon);
        drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
        drawable0.setBounds(0, 0, drawable0.getMinimumWidth(), drawable0.getMinimumHeight());
        if (soldNo == 0) {
            map.put("sell_type", 1);
            map.put("page_index", 1);
            shopSearchResultPresenter.categoryStoreSearch(map);
            sold.setTextColor(ContextCompat.getColor(getActivity(), R.color.app_green));
            sold.setCompoundDrawables(null, null, drawable0, null);
            sold.setText("销量优先");
            soldNo = 1;
            priceNo = 0;
        } else if (soldNo == 1) {
            map.put("sell_type", 2);
            map.put("page_index", 1);
            shopSearchResultPresenter.categoryStoreSearch(map);
            sold.setCompoundDrawables(null, null, drawable1, null);
            sold.setText("销量最低");
            soldNo = 2;
            priceNo = 0;
        } else {
            map.put("sell_type", 1);
            map.put("page_index", 1);
            shopSearchResultPresenter.categoryStoreSearch(map);
            sold.setCompoundDrawables(null, null, drawable0, null);
            sold.setText("销量优先");
            soldNo = 1;
            priceNo = 0;
        }
    }
}
